import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringReader;
import java.util.ArrayList;

import opennlp.tools.sentdetect.SentenceDetectorME;
import opennlp.tools.sentdetect.SentenceModel;


public class TokenizeIntoSenteceOPENNLP {

	/**
	 * @param args
	 * @throws FileNotFoundException 
	 */
	public static void main(String[] args) throws FileNotFoundException {
		// TODO Auto-generated method stub

		InputStream modelIn = new FileInputStream("en-sent.bin");

		String fileName = "/home/pjbarrio/Dataset/ACE2003/ace_phase2/data/train/9801.35.txt";
		
		try {

			String[] text = readFile(new File(fileName));
			
			SentenceModel model = new SentenceModel(modelIn);

			SentenceDetectorME sentenceDetector = new SentenceDetectorME(model);

			int count = 1;
			
			for (int j = 0; j < text.length; j++) {
				
				
				String sentences[] = sentenceDetector.sentDetect(text[j]);
				
				for (int i = 0; i < sentences.length; i++) {
					System.out.println("\nSENTENCE:  " +  count++ + "\n");
					System.out.println(sentences[i]);
				}

				
			}
			
		}
		catch (IOException e) {
		  e.printStackTrace();
		}
		finally {
		  if (modelIn != null) {
		    try {
		      modelIn.close();
		    }
		    catch (IOException e) {
		    }
		  }
		}
		
	}

	private static String[] readFile(File file) throws IOException {
		
		ArrayList<String> ret = new ArrayList<String>();
		
	    byte[] buffer = new byte[(int) file.length()];
	    BufferedInputStream f = null;
	    try {
	        f = new BufferedInputStream(new FileInputStream(file.getAbsolutePath()));
	        f.read(buffer);
	    } catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
	        if (f != null) try { f.close(); } catch (IOException ignored) { }
	    }
	    
	    String text = new String(buffer);
		
	    BufferedReader br = new BufferedReader(new StringReader(text));
	    
	    String line = br.readLine();
	    
	    String acum = "";
	    
	    while (line != null){
	    	
	    	if (line.trim().equals("")){
	    		
	    		
	    		if (!acum.trim().equals(""))
	    			ret.add(acum);
	    		
	    		acum = "";
	    	
	    	} else if (acum.trim().endsWith(".")){
	    	
	    		ret.add(acum);
	    		
	    		acum = line;
	    	
	    	}
	    	else {
	    		
	    		acum = acum + " " + line;
	    		
	    	}
	    	
	    	line = br.readLine();
	    }
	    
	    br.close();
	    
	    return ret.toArray(new String[0]);
	    
	}

	
}
